package com.jymf.login.service;

import com.jymf.common.SessionUtil;
import com.jymf.login.dao.LoginDao;
import com.jymf.model.Resource;
import com.jymf.model.Users;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author zyj
 * @create 2017/5/22
 **/
@Service
public class LoginService {

    private static Logger log = Logger.getLogger(LoginService.class);

    @Autowired
    private LoginDao logins;

    /**
     * 判断用户是否正确 并存入session
     * @author zyj
     * @date 2017/05/22
     * @param 
     * @return 
     **/
    public Users getUser(String name, String passwd, HttpServletRequest request){
        Map<String, Object> map = new HashMap<String, Object>();
        Users users = null;
        try {
            if(null!=name&&!"".equals(name)&&null!=passwd&&!"".equals(passwd)){
                map.put("user",name);
                map.put("passwd", passwd);
                users = logins.getUser(map);

                //保存session
                if(users!=null){
                    SessionUtil.setSession(request, users);
                }

            }
        }catch (Exception e){
            e.printStackTrace();
            log.error(e);
        }

        return users;
    }
    
    /**
     * @author zyj
     * @date 2017/05/23
     * @param parentId 父节点id
     * @return 子节点
     **/
    public List<Map<String, Object>> getChildrenResource(String parentId,int userId){
        List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            map.put("id", userId);
            map.put("pid", parentId);
            List<Resource> listResource = logins.getResourceByParentId(map);

            Iterator<Resource> iterator = listResource.iterator();
            while(iterator.hasNext()){
                Map<String, Object> mapValue = new HashMap<String, Object>();
                Resource resource = iterator.next();
                mapValue.put("id",resource.getId());
                mapValue.put("name",resource.getName());
                lists.add(mapValue);
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.error(e);
        }

        return  lists;
    }

    /**
     *
     * @param parentId
     * @return
     */
    public List<Map<String, Object>> getResource(String parentId, int userId){
        List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            map.put("id", userId);
            map.put("pid", null);
            List<Resource> listResource = logins.getResourceByParentId(map);
            Map<String, Object> maps = new HashMap<String, Object>();

            for(int i=0;i<listResource.size();i++){
                Resource next = listResource.get(i);
                String parentId1 = String.valueOf(next.getParentId());
                if(parentId!=null&&parentId.equals(parentId1)){
                    Map<String, Object> mapValue = new HashMap<String, Object>();
                    boolean isChildren = false;
                    int ids = next.getId();
                    mapValue.put("id", ids);
                    mapValue.put("name",next.getName());
                    mapValue.put("url",next.getUrl());
                    for(int j=0;j<listResource.size();j++){//判断是否有下级
                        Resource nexts = listResource.get(j);
                        int childrenId = nexts.getParentId();
                        if(childrenId==ids){
                            isChildren = true;
                        }
                    }
                    mapValue.put("isChildren", isChildren);

                    lists.add(mapValue);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.error(e);
        }

        return lists;
    }

    /**
     * 修改用户密码
     * @param map
     * @return
     */
    public boolean editPasswd(Map<String, Object> map){
        boolean b = false;
        try {
            logins.editPasswd(map);
            b = true;
        } catch (Exception e) {
            e.printStackTrace();
            b = false;
        }

        return b;
    }

    /**
     * 获取按钮
     * @param parentId
     * @param userId
     * @return
     */
    public List<Map<String, Object>> getResBtn(String parentId, int userId){
        List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            map.put("id", userId);
            map.put("pid", parentId);
            lists = logins.getResBtnById(map);


        } catch (Exception e) {
            e.printStackTrace();
            log.error(e);
        }

        return lists;
    }
}
